/*
 * @Author: 抱着羊啃 1437670973@qq.com
 * @Date: 2025-04-25 21:51:19
 * @LastEditors: 抱着羊啃 1437670973@qq.com
 * @LastEditTime: 2025-04-27 14:03:14
 * @FilePath: \demo1\src\main\java\org\example\demo1\servlets\ManagePage\deleteUserServlet.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package org.example.demo1.servlets.ManagePage;

import com.google.gson.Gson;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;
import org.example.demo1.DAO.UserDao;
import org.example.demo1.Entity.User;
import org.example.demo1.utils.MybatisUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/API/user/delete")
public class deleteUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        // 修复：从session获取User对象，然后获取用户名
        HttpSession session = req.getSession();
        User currentUser = (User) session.getAttribute("user");
        String currentUsername = currentUser != null ? currentUser.getUsername() : null;
        
        String[] usernames = req.getParameterValues("usernames[]");
        System.out.println(usernames);
        
        // 创建响应数据Map
        Map<String, Object> responseData = new HashMap<>();
        
        if (usernames != null && usernames.length > 0) {
            // 检查是否包含自己
            for (String username : usernames) {
                if (username != null && username.equals(currentUsername)) {
                    responseData.put("status", "error");
                    responseData.put("code", 400);
                    responseData.put("message", "不能删除自己的信息");
                    
                    // 使用Gson转换为JSON并发送响应
                    String json = new Gson().toJson(responseData);
                    out.print(json);
                    out.flush();
                    return;
                }
            }
            SqlSession sqlSession = null;
            int count = 0;
            try {
                sqlSession = MybatisUtils.getSqlSession();
                UserDao userDao = sqlSession.getMapper(UserDao.class);
                for (String username : usernames) {
                    if (username != null && !username.trim().isEmpty()) {
                        count += userDao.deleteUser(username);
                    }
                }
                sqlSession.commit();
                
                responseData.put("status", "success");
                responseData.put("code", 200);
                responseData.put("message", "成功删除 " + count + " 个用户");
            } catch (Exception e) {
                responseData.put("status", "error");
                responseData.put("code", 500);
                responseData.put("message", "服务器异常");
                e.printStackTrace();
            } finally {
                if (sqlSession != null) sqlSession.close();
            }
        } else {
            responseData.put("status", "error");
            responseData.put("code", 400);
            responseData.put("message", "用户名列表不能为空");
        }

        // 使用Gson转换为JSON并发送响应
        String json = new Gson().toJson(responseData);
        out.print(json);
        out.flush();
    }
}
